#pragma once

#include"Common.h"

#include<time.h>

#include"Visualizer.h"
#include"UnitControlerManager.h"
#include"Info.h"
#include"BuildHelper.h"
#include"ProductionManager.h"
#include"AllUnitTypes.h"
#include"SpendType.h"

#include"PlayerUnitTypeData.h"

#include"ArmyManager.h"

class SCHW7 : public AIModule
{
public:
	SCHW7()
		:_started(false),
		_initialized(false),
		_gameType(GameTypes::None)
	{
		srand(time(NULL));
	}
	void onStart()
	{
		Broodwar->setLocalSpeed(50);
		Broodwar->setTextSize(0);
		Broodwar->enableFlag(Flag::UserInput);
		Broodwar->enableFlag(Flag::CompleteMapInformation);
		_gameType = Broodwar->getGameType();
		if(_gameType == GameTypes::Melee)
		{
			//BWAPI::Broodwar->setCommandOptimizationLevel(1);

			BWTA::readMap();
			BWTA::analyze();

			Macro::InitializeUnitTypes();

			OpeningBuild();
		}
		_started = true;
	}
	void onFrame()
	{
		if(_started)
		{
			if(_gameType == GameTypes::Melee)
			{
				if(!_initialized)
				{
					Info::Instance().Initialize();
					BuildHelper::Instance().Initialize();
					Macro::InitSpendTypeTable();
					ArmyManager.Initialize(_gameType);
					PlayerUnitTypeInfo.Initialize();
					_initialized = true;
				}
				else
				{
					Visualizer::Draw();
					Info::Instance().OnFrame();
					ProductionManager::Instance().OnFrame();
					UnitControlerManager::Instance().OnFrame();
					ArmyManager.OnFrame();
					PlayerUnitTypeInfo.OnFrame();
				}
			}
			else
			{
				if(!_initialized)
				{
					ArmyManager.Initialize(_gameType);
					PlayerUnitTypeInfo.Initialize();
					_initialized = true;
				}
				else
				{
					Visualizer::Draw();
					UnitControlerManager::Instance().OnFrame();
					ArmyManager.OnFrame();
					PlayerUnitTypeInfo.OnFrame();
				}
			}
		}
	}
	void onUnitCreate(Unit* unit)
	{
		if(unit->getPlayer() == Broodwar->self())
		{
			UnitControlerManager::Instance().OnUnitCreate(unit);
		}
		ArmyManager.OnUnitCreate(unit);
	}
	void onUnitMorph(Unit* unit)
	{
		if(_gameType == GameTypes::Melee)
		{
			Info::Instance().OnUnitMorph(unit);
		}
		ArmyManager.OnUnitMorph(unit);
	}
	void onUnitDestroy(Unit* unit)
	{
	}

	void onSendText(string text)
	{
		if(text == "s")
		{
			ArmyManager.SetWantdraw(true);
		}
	}
private:
	void OpeningBuild()
	{
		list<ProductionTask> tasks;

		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Overlord));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Spawning_Pool , 0));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));

		tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));

		tasks.push_back(ProductionTask(UnitTypes::Zerg_Hatchery , 1));

		tasks.push_back(ProductionTask(UnitTypes::Zerg_Hatchery , 1));

		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));
		tasks.push_back(ProductionTask(UnitTypes::Zerg_Drone));

		for(int i=0;i<1;++i)
		{
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Overlord));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
			tasks.push_back(ProductionTask(UnitTypes::Zerg_Zergling));
		}

		ProductionManager::Instance().AddTasks(tasks);
	}
	//--
	bool _started;
	bool _initialized;
	GameType _gameType;
};